package com.nageoffer.shortlink.admin.remote.dto;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nageoffer.shortlink.admin.common.convention.result.Result;
import com.nageoffer.shortlink.admin.remote.dto.req.*;
import com.nageoffer.shortlink.admin.remote.dto.resp.ShortLinkCreateRespDTO;
import com.nageoffer.shortlink.admin.remote.dto.resp.ShortLinkGroupCountQueryRespDTO;
import com.nageoffer.shortlink.admin.remote.dto.resp.ShortLinkPageRespDTO;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 短链接中台远程调用服务
 */
public interface ShortLinkRemoteService {

    default Result<ShortLinkCreateRespDTO> createShortLink(ShortLinkCreateReqDTO requestParam){
        String resultBodyStr = HttpUtil.post("http://127.0.0.1:8001/api/short-link/v1/create", JSON.toJSONString(requestParam));
        /**
         * 这个TypeReference是干什么用的？
         *  当使用Json进行反序列化的时候，这个Result 里面有泛型 T
         *  如果我只传一个result进去它不知道我的泛型是什么数据
         *  TypeReference就是用来进行转换 告诉它里面是什么，这里没有具体实现，盲猜是某种隐式转换
         */
        return JSON.parseObject(resultBodyStr, new TypeReference<>(){});
    }

    default Result<IPage<ShortLinkPageRespDTO>> pageShortLink(String gid, Long current, Long size){
        //TODO: 为什么这里要用map
        HashMap<String, Object> requestMap = new HashMap<>();
        requestMap.put("gid", gid);
        requestMap.put("current", current);
        requestMap.put("size", size);
        String requestPageStr = HttpUtil.get("http://127.0.0.1:8001/api/short-link/v1/page", requestMap);
        return JSON.parseObject(requestPageStr, new TypeReference<>(){});
    }

    default Result<List<ShortLinkGroupCountQueryRespDTO>> listGroupShortLinkCount(List<String> requestParam) {
//        return Results.success(shortLinkService.listGroupShortLinkCount(requestParam));
        HashMap<String, Object> requestMap = new HashMap<>();
        requestMap.put("requestParam", requestParam);
        String requestPageStr = HttpUtil.get("http://127.0.0.1:8001/api/short-link/v1/count", requestMap);
        return JSON.parseObject(requestPageStr, new TypeReference<>(){});
    }


    default void updateShortLink(ShortLinkUpdateReqDTO requestParam) {
        HttpUtil.post("http://127.0.0.1:8001/api/short-link/v1/update", JSON.toJSONString(requestParam));
//        return JSON.parseObject(requestPageStr, new TypeReference<>(){});
    }

    default Result<String> getTitleByUrl(String url) {
        String resultStr = HttpUtil.get("http://127.0.0.1:8001/api/short-link/v1/title?url=" + url);
        return JSON.parseObject(resultStr, new TypeReference<>(){});
    }

    default void saveRecycleBin(RecycleBinSaveReqDTO requestParam) {
        // 第一种，使用map对参数进行映射
//        HashMap<String, Object> requestMap = new HashMap<>();
//        requestMap.put("requestParam", requestParam);
//        HttpUtil.post("http://127.0.0.1:8001/api/short-link/v1/recycle-bin/save", requestMap);
        // 第二种，将参数序列化为string
        HttpUtil.post("http://127.0.0.1:8001/api/short-link/v1/recycle-bin/save", JSON.toJSONString(requestParam));
    }

    default Result<IPage<ShortLinkPageRespDTO>> pageRecycleShortLink(String gid, Long current, Long size){
        //TODO: 为什么这里要用map
        HashMap<String, Object> requestMap = new HashMap<>();
        requestMap.put("gid", gid);
        requestMap.put("current", current);
        requestMap.put("size", size);
        String requestPageStr = HttpUtil.get("http://127.0.0.1:8001/api/short-link/v1/recycle-bin/page", requestMap);
        return JSON.parseObject(requestPageStr, new TypeReference<>(){});
    }

    default void recoverRecycleBin(RecycleBinRecoverReqDTO requestparam){
        HttpUtil.post("http://127.0.0.1:8001/api/short-link/v1/recycle-bin/recover", JSON.toJSONString(requestparam));
    }

    default void removeRecycleBin(RecycleBinRemoveReqDTO requestparam){
        HttpUtil.post("http://127.0.0.1:8001/api/short-link/v1/recycle-bin/remove", JSON.toJSONString(requestparam));
    }

}
